package main

import (
	"FlowMaster/application"
	"FlowMaster/config"
	"FlowMaster/logger"
	"FlowMaster/model"
	"FlowMaster/service"
	"fmt"
	"time"
)

var loggerI = logger.Get()

const minimalRealCount = 5

func main() {
	c, err := config.InitConfig()
	if err != nil {
		loggerI.Error("获取配置失败", err)
		return
	}
	loggerI.Info(fmt.Sprintf("定时任务 %d 秒", c.Interval.NewTicker))
	ticker := time.NewTicker(time.Second * time.Duration(c.Interval.NewTicker))

	defer ticker.Stop()
	switch c.Active.Active {
	case "PFlib":
		loggerI.Info("获取数图客流系统")
		pFlib(ticker, c)
	case "CustCount":
		loggerI.Info("获取图创客流数据")
		custCount(ticker, c)
	default:
		loggerI.Error("未设置获取数据来源")
		return
	}

}

func custCount(ticker *time.Ticker, c model.Config) {
	for range ticker.C {
		loggerI.Info("获取图创客流数据")
		custCountResponse, err := service.CustCountMakeRequest(c.CustCount.Url, c.CustCount.Key, c.CustCount.Cameraid)
		if err != nil {
			loggerI.Error("获取客流数据失败", err)
		}
		totle := custCountResponse.TodayIn
		out := custCountResponse.TodayOut
		real := totle - out
		if real <= 0 {
			real = minimalRealCount
		}
		data := fmt.Sprintf("今日入园人数:%d, 今日出园人数:%d, 今日入园人数:%d", totle, out, real)
		loggerI.Info(data)
		err = application.UploadDataRun(c.Yunyouqilu, totle, out, real)
		if err != nil {
			loggerI.Error("上传数据失败", err)
		}
		loggerI.Info("上传数据", data)
	}
}

// 数图客流系统
func pFlib(ticker *time.Ticker, c model.Config) {
	for range ticker.C {
		loggerI.Info("获取数图客流系统")
		totle, out := application.PFlib()
		real := totle - out
		if real <= 0 {
			real = minimalRealCount
		}
		data := fmt.Sprintf("今日入园人数:%d, 今日出园人数:%d, 今日入园人数:%d", totle, out, real)
		loggerI.Info(data)
		err := application.UploadDataRun(c.Yunyouqilu, totle, out, real)
		if err != nil {
			loggerI.Error("上传数据失败", err)
		}
		loggerI.Info("上传数据", data)
	}
}
